/*-------------------------------------------------------------------------
File Name: Scpi.c
Function Description: the scpi command define.
Author: ray.gong (2012-11-10)
Modify history:
-------------------------------------------------------------------------*/

#include <types.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <bus/sci/sci.h>
#include <bus/dog/dog.h>
#include <hld/hld_dev.h>
#include "scpi.h"
#include "scpi_cmd_drv.h"
#include "scpi_cmd.h"

stDbgCmd g_stDbgCmdStr;

stDbgCmdNode g_stDbgCmdNode[USER_CMD_COUNT_MAX+1]=
{
	{"help","list all cmd instroduce;",StrCmdHelp},
	{"setdbgprint","set module debug printf level,0 is no print;",StrCmdSetDbgPrint},
	{"rdreg","read reg;",StrCmdReadReg},
	{"wtreg","write reg;",StrCmdWriteReg},			
	{"rdmem","read memory;",StrCmdReadMem},
	{"wtmem","write memory;",StrCmdWriteMem},
	{"taskinfo","get system task status information;",StrCmdGetTaskInfo},
	{"mallocdump","take memory space for module dump;",StrCmdGetDumpMem},
	{"freedump","release memory space for module dump;",StrCmdReleaseDumpMem},
	{"NULL","NULL",NULL}	
};

/*-------------------------------------------------------------------------
Function Name: StrCmdHelp
Function Description: list all support command.
Author: ray.gong (2012-11-10)
Input parameter: NONE;
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/

UINT32 StrCmdHelp(UINT32 *argc,CHAR **argv,CHAR *szOutPut)
{
	UINT32 i=0;
	UINT32 j=0;
	INT32 iSpaceLen=0;
	UINT32 uiTotalLen=0;
	UINT32 uiRtn=-1;
	
	UINT32 iCnt=sizeof(g_stDbgCmdNode)/sizeof(stDbgCmdNode);
	
	for(i=0;((i<iCnt)
		&&(0!=strcmp(g_stDbgCmdNode[i].acCmdID,"NULL"))
		&&(0!=strcmp(g_stDbgCmdNode[i].acCmdID,"")));i++)
	{
		uiTotalLen += strlen("\r\n")+ strlen(g_stDbgCmdNode[i].acCmdID);
		if(uiTotalLen>UART_CMD_OUTPUT_LEN_MAX)
		{
			uiRtn=-1; 
			goto END_CMD_PROC;
		}
		strcat(szOutPut,"\r\n");
		strcat(szOutPut,g_stDbgCmdNode[i].acCmdID);

		iSpaceLen=(UART_CMD_ID_LEN_MAX-strlen(g_stDbgCmdNode[i].acCmdID))>0
			?(UART_CMD_ID_LEN_MAX-strlen(g_stDbgCmdNode[i].acCmdID)):0;
		
		uiTotalLen += iSpaceLen;
		if(uiTotalLen>UART_CMD_OUTPUT_LEN_MAX)
		{
			uiRtn=-1; 
			goto END_CMD_PROC;
		}
		
		for(j=0;j<iSpaceLen;j++)
		{
			strcat(szOutPut," ");
		}

		uiTotalLen += strlen(g_stDbgCmdNode[i].acCmdInstuction);
		if(uiTotalLen>UART_CMD_OUTPUT_LEN_MAX)
		{
			uiRtn=-1; 
			goto END_CMD_PROC;
		}	
		
		strcat(szOutPut,g_stDbgCmdNode[i].acCmdInstuction);
		uiRtn=0;
	}

END_CMD_PROC:

	return uiRtn;
}

/*-------------------------------------------------------------------------
Function Name: StrCmdSetDbgPrint
Function Description: set soft module debug print level,0 is no print .
Author: ray.gong (2012-11-10)
Input parameter: NONE;
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 StrCmdSetDbgPrint(UINT32 *argc,CHAR **argv,CHAR *szOutPut)
{
	UINT32 uiMdlId=0;
	UINT32 uiPrtLevel=0;
	UINT32 uiRtn=-1;
	
	if((NULL==argc)||(NULL==argv)||(*argc!=2))
	{
		return -1;
	}

	if((1!=sscanf(argv[0],"%d",&uiMdlId)))
	{
		return -1;
	}

	if((1!=sscanf(argv[1],"%d",&uiPrtLevel)))
	{
		return -1;
	}		

	if(0!=drvSetModulePrintLevel(uiMdlId,uiPrtLevel))
	{
		return -1;
	}
	snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"set modlue [%d] to printf level [%d]",uiMdlId,uiPrtLevel);
	return 0;
}

/*-------------------------------------------------------------------------
Function Name: StrCmdGetDumpMem
Function Description: get memory space for dump .
Author: ray.gong (2012-11-10)
Input parameter: NONE;
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 StrCmdGetDumpMem(UINT32 *argc,CHAR **argv,CHAR *szOutPut)
{
	UINT32 uiMdlId=0;
	UINT32 uiMemLen=0;
	UINT32 uiAddr=0;
	UINT32 uiRtn=-1;
	
	if((NULL==argc)||(NULL==argv)||(*argc!=2))
	{
		return -1;
	}

	if((1!=sscanf(argv[0],"%d",&uiMdlId)))
	{
		return -1;
	}

	if((1!=sscanf(argv[1],"%d",&uiMemLen)))
	{
		return -1;
	}		

	if(0!=drvGetDumpMem(uiMdlId,uiMemLen,&uiAddr))
	{
		return -1;
	}
	snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"get memory len [0x%08x] at addr[0x%08x] for module [%d] ",uiMemLen,uiAddr,uiMdlId);
	
	return 0;
}
/*-------------------------------------------------------------------------
Function Name: StrCmdReleaseDumpMem
Function Description: release memory space for dump .
Author: ray.gong (2012-11-10)
Input parameter: NONE;
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 StrCmdReleaseDumpMem(UINT32 *argc,CHAR **argv,CHAR *szOutPut)
{
	UINT32 uiMdlId=0;
	UINT32 uiMemLen=0;
	UINT32 uiAddr=0;
	UINT32 uiRtn=-1;
	
	if((NULL==argc)||(NULL==argv)||(*argc!=1))
	{
		return -1;
	}

	if((1!=sscanf(argv[0],"%d",&uiMdlId)))
	{
		return -1;
	}

	if(0!=drvReleaseDumpMem(uiMdlId))
	{
		return -1;
	}
	
	snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"release dump memory for module [%d] ",uiMdlId);
	
	return 0;
}

//extern TCB *g_tcb_table;
/*-------------------------------------------------------------------------
Function Name: StrCmdGetTaskInfo
Function Description: get task run status info .
Author: ray.gong (2012-11-10)
Input parameter: NONE;
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 StrCmdGetTaskInfo(UINT32 *argc,CHAR **argv,CHAR *szOutPut)
{
	UINT32 i=0;
	UINT32 j=0;
	INT32 iSpaceLen=0;
	UINT32 uiTotalLen=0;
	UINT32 uiRtn=-1;
	CHAR acTmp[UART_CMD_OUTPUT_LEN_MAX]={0};
	CHAR acTaskName[4]={0};
	UINT32 iCnt=0;
	#if 0
	for(i=0;((g_tcb_table[i].name[0]!=0));i++)
	{
		for(j=0;j<3;j++)
		{
			acTaskName[j]=g_tcb_table[i].name[j];
		}
		
		uiTotalLen +=strlen("NAME=\r\n") + strlen(acTaskName);
		if(uiTotalLen>UART_CMD_OUTPUT_LEN_MAX)
		{
			uiRtn=-1; 
			goto END_CMD_PROC;
		}
		sprintf(szOutPut,"NAME=%s\r\n",acTaskName);


		uiTotalLen +=strlen("SP=[XXXXXXXX][XXXXXXXX][XXXXXXXX]\r\n");
		if(uiTotalLen>UART_CMD_OUTPUT_LEN_MAX)
		{
			uiRtn=-1; 
			goto END_CMD_PROC;
		}
		sprintf(acTmp,"SP=[%08x][%08x][%08x]\r\n",g_tcb_table[i].sp,g_tcb_table[i].stackbegin,g_tcb_table[i].stackend);
		strcat(szOutPut,acTmp);

		uiTotalLen +=strlen("STATUS=[XXXX]\r\n");
		if(uiTotalLen>UART_CMD_OUTPUT_LEN_MAX)
		{
			uiRtn=-1; 
			goto END_CMD_PROC;
		}
		sprintf(acTmp,"STATUS=[%d]\r\n",g_tcb_table[i].state);
		strcat(szOutPut,acTmp);
		uiRtn=0;
	}

END_CMD_PROC:
	#endif
	return uiRtn;
}

/*-------------------------------------------------------------------------
Function Name: StrCmdReadMem
Function Description: read memory  .
Author: ray.gong (2012-11-10)
Input parameter: NONE;
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 StrCmdReadMem(UINT32 *argc,CHAR **argv,CHAR *szOutPut)
{
	UINT32 uiMode=0;
	UINT32 uiAddr=0;
	UINT32 uiVal=0;
	UINT32 uiRtn=-1;
	
	if((NULL==argc)||(NULL==argv)||(*argc!=2))
	{
		return -1;
	}

	if((1!=sscanf(argv[0],"%d",&uiMode)))
	{
		return -1;
	}

	if(0!=drvStr2Hex(argv[1],&uiAddr))
	{
		if((1!=sscanf(argv[1],"%d",&uiAddr)))
		{
			return -1;
		}		
	}


	if(0!=drvReadMem(uiMode,uiAddr,&uiVal))
	{
		return -1;
	}

	switch(uiMode)
	{
		case BIT_8:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Addr:[0x%08x]=[0x%02x]",uiAddr,uiVal);
			uiRtn=0;
			break;
		case BIT_16:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Addr:[0x%08x]=[0x%04x]",uiAddr,uiVal);
			uiRtn=0;
			break;
		case BIT_32:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Addr:[0x%08x]=[0x%08x]",uiAddr,uiVal);
			uiRtn=0;
			break;
		default:
			uiRtn=-1;
			break;
	}

	return uiRtn;
}
/*-------------------------------------------------------------------------
Function Name: StrCmdWriteMem
Function Description: write memory  .
Author: ray.gong (2012-11-10)
Input parameter: NONE;
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 StrCmdWriteMem(UINT32 *argc,CHAR **argv,CHAR *szOutPut)
{
	UINT32 uiMode=0;
	UINT32 uiAddr=0;
	UINT32 uiVal=0;
	UINT32 uiRtn=0;
	
	if((NULL==argc)||(NULL==argv)||(*argc!=3))
	{
		return -1;
	}

	if((1!=sscanf(argv[0],"%d",&uiMode)))
	{
		return -1;
	}
	
	if(0!=drvStr2Hex(argv[1],&uiAddr))
	{
		if((1!=sscanf(argv[1],"%d",&uiAddr)))
		{
			return -1;
		}		
	}
	
	if(0!=drvStr2Hex(argv[2],&uiVal))
	{
		if((1!=sscanf(argv[2],"%d",&uiVal)))
		{
			return -1;
		}		
	}

	if(0!=drvWriteMem(uiMode,uiAddr,uiVal))
	{
		return -1;
	}

	switch(uiMode)
	{
		case BIT_8:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Write Addr:[0x%08x]=[0x%02x] Successed!",uiAddr,uiVal);
			uiRtn=0;
			break;
		case BIT_16:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Write Addr:[0x%08x]=[0x%04x] Successed!",uiAddr,uiVal);
			uiRtn=0;
			break;
		case BIT_32:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Write Addr:[0x%08x]=[0x%08x] Successed!",uiAddr,uiVal);
			uiRtn=0;
			break;
		default:
			uiRtn=-1;
			break;
	}

	return 0;
}

/*-------------------------------------------------------------------------
Function Name: StrCmdReadReg
Function Description: read registor  .
Author: ray.gong (2012-11-10)
Input parameter: NONE;
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 StrCmdReadReg(UINT32 *argc,CHAR **argv,CHAR *szOutPut)
{
	UINT32 uiMode=0;
	UINT32 uiAddr=0;
	UINT32 uiVal=0;
	UINT32 uiRtn=-1;
	
	if((NULL==argc)||(NULL==argv)||(*argc!=2))
	{
		return -1;
	}

	if((1!=sscanf(argv[0],"%d",&uiMode)))
	{
		return -1;
	}

	if(0!=drvStr2Hex(argv[1],&uiAddr))
	{
		if((1!=sscanf(argv[1],"%d",&uiAddr)))
		{
			return -1;
		}		
	}

	if(0!=drvReadReg(uiMode,uiAddr,&uiVal))
	{
		return -1;
	}

	switch(uiMode)
	{
		case BIT_8:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Addr:[0x%08x]=[0x%02x]",uiAddr,uiVal);
			uiRtn=0;
			break;
		case BIT_16:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Addr:[0x%08x]=[0x%04x]",uiAddr,uiVal);
			uiRtn=0;
			break;
		case BIT_32:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Addr:[0x%08x]=[0x%08x]",uiAddr,uiVal);
			uiRtn=0;
			break;
		default:
			uiRtn=-1;
			break;
	}

	return uiRtn;
}

/*-------------------------------------------------------------------------
Function Name: StrCmdWriteReg
Function Description: write registor  .
Author: ray.gong (2012-11-10)
Input parameter: NONE;
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 StrCmdWriteReg(UINT32 *argc,CHAR **argv,CHAR *szOutPut)
{
	UINT32 uiMode=0;
	UINT32 uiAddr=0;
	UINT32 uiVal=0;
	UINT32 uiRtn=-1;
	
	if((NULL==argc)||(NULL==argv)||(*argc!=3))
	{
		return -1;
	}

	if((1!=sscanf(argv[0],"%d",&uiMode)))
	{
		return -1;
	}

	if(0!=drvStr2Hex(argv[1],&uiAddr))
	{
		if((1!=sscanf(argv[1],"%d",&uiAddr)))
		{
			return -1;
		}		
	}
	
	if(0!=drvStr2Hex(argv[2],&uiVal))
	{
		if((1!=sscanf(argv[2],"%d",&uiVal)))
		{
			return -1;
		}		
	}


	if(0!=drvWriteReg(uiMode,uiAddr,uiVal))
	{
		return -1;
	}

	switch(uiMode)
	{
		case BIT_8:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Write Addr:[0x%08x]=[0x%02x] Successed!",uiAddr,uiVal);
			uiRtn=0;
			break;
		case BIT_16:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Write Addr:[0x%08x]=[0x%04x] Successed!",uiAddr,uiVal);
			uiRtn=0;
			break;
		case BIT_32:
			snprintf(szOutPut,UART_CMD_OUTPUT_LEN_MAX,"Write Addr:[0x%08x]=[0x%08x] Successed!",uiAddr,uiVal);
			uiRtn=0;
			break;
		default:
			uiRtn=-1;
			break;
	}

	return uiRtn;
}


